# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from common.models.agents import AgentSquareModel, AgentKnowModel, AgentRobotModel
from common.models.agents import AgentChatRoomingModel
from common.models.agents import AgentChatSessionModel
from common.models.agents import AgentChatRecordsModel
from common.models.agents import AgentRobotTeamedModel
from common.models.agents import AgentRobotCollectModel
from common.models.agents import AgentRobotPublishModel
from common.models.agents import AgentRobotInstructModel
from common.models.agents import AgentKnowTeamedModel
from common.models.agents import AgentKnowArchiveModel
from common.models.agents import AgentKnowSplittingModel
from common.postgres.public import PgKnowledgeModel
from common.postgres.public import PgAttachmentModel


class CleanLogic:
    @classmethod
    async def clean_robot(cls, robot_id: int):
        """ 清理智能体关联表 """
        # 当前时间
        curr_time = int(time.time())

        # 智能体删除
        await AgentRobotModel.filter(id=robot_id).update(
            is_delete=1,
            delete_time=int(time.time())
        )

        # 删除协作团队
        await AgentRobotTeamedModel.filter(id=robot_id).delete()

        # 删除快捷指令
        await AgentRobotInstructModel.filter(id=robot_id).delete()

        # 删除发布记录
        await AgentRobotPublishModel.filter(robot_id=robot_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除应用窗口
        await AgentChatRoomingModel.filter(robot_id=robot_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除会话窗口
        await AgentChatSessionModel.filter(robot_id=robot_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除对话记录
        await AgentChatRecordsModel.filter(robot_id=robot_id, is_delete=0).update(
            user_delete=1,
            delete_time=curr_time
        )

        # 删除广场分享
        await AgentSquareModel.filter(robot_id=robot_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除广场收藏
        await AgentRobotCollectModel.filter(robot_id=robot_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

    @classmethod
    async def clean_know(cls, know_id: int):
        """ 清理知识库管理表 """
        # 当前时间
        curr_time = int(time.time())

        # 删除知识库
        await AgentKnowModel.filter(id=know_id).update(
            is_delete=1,
            delete_time=int(time.time())
        )

        # 删除协作团队
        await AgentKnowTeamedModel.filter(id=int(know_id)).delete()

        # 删除文本拆分
        await AgentKnowSplittingModel.filter(know_id=know_id).delete()

        # 删除知识文档
        await AgentKnowArchiveModel.filter(know_id=know_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除知识数据
        await PgKnowledgeModel.filter(know_id=know_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除知识附件
        await PgAttachmentModel.filter(know_id=int(know_id), is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

    @classmethod
    async def clean_know_archive(cls, archive_id: int):
        """ 清理文档关联表 """
        # 当前时间
        curr_time = int(time.time())

        # 删除知识文档
        await AgentKnowArchiveModel.filter(id=archive_id).update(
            is_delete=1,
            delete_time=int(time.time())
        )

        # 删除文本拆分
        await AgentKnowSplittingModel.filter(archive_id=archive_id).delete()

        # 删除知识数据
        await PgKnowledgeModel.filter(archive_id=archive_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )

        # 删除知识附件
        await PgAttachmentModel.filter(archive_id=archive_id, is_delete=0).update(
            is_delete=1,
            delete_time=curr_time
        )
